home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / SimpleExample.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  154 lines

  1. /*
  2.  * @(#)SimpleExample.java    1.20 98/03/20
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. import java.awt.*;
  22. import java.awt.event.*;
  23. import com.sun.java.swing.*;
  24.  
  25. /**
  26.  * An application that displays a JButton and several JRadioButtons.
  27.  * The JRadioButtons determine the look and feel used by the application.
  28.  */
  29. public class SimpleExample extends JPanel {
  30.     static JFrame frame;
  31.  
  32.     static String metal= "Metal";
  33.     static String metalClassName = "com.sun.java.swing.plaf.metal.MetalLookAndFeel";
  34.  
  35.     static String motif = "Motif";
  36.     static String motifClassName = 
  37.         "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
  38.  
  39.     static String windows = "Windows";
  40.     static String windowsClassName = 
  41.         "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
  42.  
  43.     JRadioButton metalButton, motifButton, windowsButton;
  44.  
  45.     public SimpleExample() {
  46.     // Create the buttons.
  47.     JButton button = new JButton("Hello, world");
  48.         button.setMnemonic('h'); //for looks only; button does nada
  49.  
  50.     metalButton = new JRadioButton(metal);
  51.         metalButton.setMnemonic('o'); 
  52.     metalButton.setActionCommand(metalClassName);
  53.  
  54.     motifButton = new JRadioButton(motif);
  55.         motifButton.setMnemonic('m'); 
  56.     motifButton.setActionCommand(motifClassName);
  57.  
  58.     windowsButton = new JRadioButton(windows);
  59.         windowsButton.setMnemonic('w'); 
  60.     windowsButton.setActionCommand(windowsClassName);
  61.  
  62.     // Group the radio buttons.
  63.     ButtonGroup group = new ButtonGroup();
  64.     group.add(metalButton);
  65.     group.add(motifButton);
  66.     group.add(windowsButton);
  67.  
  68.         // Register a listener for the radio buttons.
  69.     RadioListener myListener = new RadioListener();
  70.     metalButton.addActionListener(myListener);
  71.     motifButton.addActionListener(myListener);
  72.     windowsButton.addActionListener(myListener);
  73.  
  74.     add(button);
  75.     add(metalButton);
  76.     add(motifButton);
  77.     add(windowsButton);
  78.     }
  79.  
  80.  
  81.     /** An ActionListener that listens to the radio buttons. */
  82.     class RadioListener implements ActionListener {
  83.     public void actionPerformed(ActionEvent e) {
  84.         String lnfName = e.getActionCommand();
  85.  
  86.             try {
  87.         UIManager.setLookAndFeel(lnfName);
  88.         SwingUtilities.updateComponentTreeUI(frame);
  89.         frame.pack();
  90.             } 
  91.         catch (Exception exc) {
  92.         JRadioButton button = (JRadioButton)e.getSource();
  93.         button.setEnabled(false);
  94.         updateState();
  95.                 System.err.println("Could not load LookAndFeel: " + lnfName);
  96.             }
  97.         
  98.     }
  99.     }
  100.  
  101.     public void updateState() {
  102.      String lnfName = UIManager.getLookAndFeel().getClass().getName();
  103.      if (lnfName.indexOf(metal) >= 0) {
  104.          metalButton.setSelected(true);
  105.      } else if (lnfName.indexOf(windows) >= 0) {
  106.          windowsButton.setSelected(true);
  107.      } else if (lnfName.indexOf(motif) >= 0) {
  108.          motifButton.setSelected(true);
  109.      } else {
  110.          System.err.println("SimpleExample is using an unknown L&F: " + lnfName);
  111.      }
  112.     }
  113.  
  114.     public static void main(String s[]) {
  115.     /* 
  116.        NOTE: By default, the look and feel will be set to the
  117.        Cross Platform Look and Feel (which is currently Metal).
  118.        The user may someday be able to override the default
  119.        via a system property. If you as the developer want to
  120.        be sure that a particular L&F is set, you can do so
  121.        by calling UIManager.setLookAndFeel(). For example, the
  122.        first code snippet below forcibly sets the UI to be the
  123.        System Look and Feel. The second code snippet forcibly
  124.        sets the look and feel to the Cross Platform L&F.
  125.  
  126.        Snippet 1:
  127.           try {
  128.               UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  129.           } catch (Exception exc) {
  130.               System.err.println("Error loading L&F: " + exc);
  131.           }
  132.  
  133.        Snippet 2:
  134.           try {
  135.               UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  136.           } catch (Exception exc) {
  137.               System.err.println("Error loading L&F: " + exc);
  138.               }
  139.     */
  140.  
  141.     SimpleExample panel = new SimpleExample();
  142.     
  143.     frame = new JFrame("SimpleExample");
  144.     frame.addWindowListener(new WindowAdapter() {
  145.         public void windowClosing(WindowEvent e) {System.exit(0);}
  146.     });
  147.     frame.getContentPane().add("Center", panel);
  148.     frame.pack();
  149.     frame.setVisible(true);
  150.     
  151.     panel.updateState();
  152.     }
  153. }
  154.